package com.qkm.xinguan.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qkm.xinguan.constant.SystemConst;
import com.qkm.xinguan.domain.entity.Complaint;
import com.qkm.xinguan.domain.entity.Tourist;
import com.qkm.xinguan.domain.infrastructure.utils.SpringComponentUtil;
import com.qkm.xinguan.domain.transform.dto.ComplaintDTO;
import com.qkm.xinguan.domain.transform.dto.TouristDTO;
import com.qkm.xinguan.exception.BindingResultException;
import com.qkm.xinguan.exception.BusinessException;
import com.qkm.xinguan.repository.ComplaintRepository;
import com.qkm.xinguan.response.Result;
import com.qkm.xinguan.response.ResultCode;
import com.qkm.xinguan.vo.ComplaintVo;
import com.qkm.xinguan.vo.HouseInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * 投诉表
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2022-01-28 11:58:26
 */
@RestController
public class ComplaintController implements IComplaintController {
    private final ComplaintRepository complaintRepository;

    @Autowired
    public ComplaintController(ComplaintRepository complaintRepository) {

        this.complaintRepository = complaintRepository;
    }

    @Override
    public Result addComplaint(ComplaintDTO complaintDTO, BindingResult result) {
        if (result.hasErrors()) {
            throw new BindingResultException(result);
        }
        boolean b = complaintRepository.save(executeEntity(complaintDTO, null));
        if (b) {
            return Result.ok().message("举报成功");
        } else {
            throw new BusinessException(ResultCode.ADD_Complaint.getCode(), "举报失败");
        }
    }

    @Override
    public Result update(Long id) {
        int res=complaintRepository.updateDeal(id);
        if (res == 1) {
            return Result.ok().message("下架成功");
        }
        throw new BusinessException(ResultCode.EXAMINE_FAIL_COMPLAINT.getCode(), "下架失败");
    }

    @Override
    public Result getAllList(Integer page, Integer size) {
        Page<ComplaintVo> pageInfo = complaintRepository.getComplaintListPage(page, size);
        return Result.ok().data(pageInfo);
    }

    @Override
    public Result getAllListById(Long id) {
        return complaintRepository.getAllListById(id);
    }

    @Override
    public Result deleteById(Long id) {
        return complaintRepository.deleteById(id);
    }

    /**
     * 构建实体类对象
     *
     * @param complaintDTO 用户传输对象
     * @return User
     */
    public static Complaint executeEntity(ComplaintDTO complaintDTO, Long id) {
        Complaint complaint = new Complaint();
        BeanUtils.copyProperties(complaintDTO, complaint);
        complaint.setId(id);
        complaint.setDeal(0);
        return complaint;
    }
}
